/**  
 * All rights Reserved, Designed By www.tydic.com
 * @Title:  AccountController.java   
 * @Package com.axin.controller   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: axin     
 * @date:   2019年4月2日 下午10:46:48   
 * @version V1.0 
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
package com.axin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.axin.common.entity.ApiToken;
import com.axin.common.entity.DataGridResult;
import com.axin.common.entity.JsonResult;
import com.axin.common.entity.response.Accounts;
import com.axin.common.entity.response.Balance;
import com.axin.common.entity.response.BalanceBean;
import com.axin.common.exception.ApiException;
import com.axin.common.exception.UnTokenException;
import com.axin.service.AccountService;

/**   
 * @Description: 账户相关 
 * @ClassName:  AccountController
 * @author:  Axin 
 * @date:   2019年4月2日 下午10:46:48   
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
@Controller
@RequestMapping("/account")
public class AccountController {
	
	
	@Autowired
	private AccountService accountService;
	
	/**
	 * 获取账户信息
	 * @Title: getAccounts   
	 * @return JsonResult
	 * @throws
	 */
	@RequestMapping(value="/getAccounts",method=RequestMethod.GET)
	@ResponseBody
	public JsonResult getAccounts(HttpServletRequest request){
		ApiToken token = (ApiToken)request.getAttribute("apiToken");
		if(token == null){
			return new JsonResult(new UnTokenException("您尚未设置密钥，不能处理您的请求！"));
		}
		try {
			List<Accounts> accounts = this.accountService.getAccounts(token);
			return new JsonResult(accounts);
		} catch (ApiException e) {
			e.printStackTrace();
			return new JsonResult(e);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult(e);
		}
	}
	
	@RequestMapping(value="/getBalance",method=RequestMethod.GET)
	@ResponseBody
	public JsonResult getBalance(@RequestParam(value="id",required=true)Integer id,HttpServletRequest request){
		ApiToken token = (ApiToken)request.getAttribute("apiToken");
		if(token == null){
			return new JsonResult(new UnTokenException("您尚未设置密钥，不能处理您的请求！"));
		}
		try {
			Balance<List<BalanceBean>> balance = this.accountService.getBalance(token, id);
			return new JsonResult(balance);
		} catch (ApiException e) {
			e.printStackTrace();
			return new JsonResult(e);
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonResult(e);
		}
	}

	
	@RequestMapping(value="/getBalanceList",method=RequestMethod.GET)
	@ResponseBody
	public DataGridResult getBalanceList(Integer id,HttpServletRequest request){
		ApiToken token = (ApiToken)request.getAttribute("apiToken");
		if(token == null){
			return new DataGridResult(0,null);
		}
		try {
			DataGridResult result = this.accountService.getBalanceList(token,id);
			return result;
		} catch (ApiException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new DataGridResult(0,null);
	}

	
}
